package com.xuzhen.init;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xuzhen.init.impl.UserInitialization;
import com.xuzhen.service.UserService;

public class ApplicationBuilder {
	private ApplicationContext applicationContext;

	private List<Initialization> inits = new ArrayList<Initialization>();

	public ApplicationBuilder() {
		applicationContext = new ClassPathXmlApplicationContext(
				new String[] { "applicationContext.xml" });
		addInitialization(new UserInitialization(
				(UserService) getApplicationContext().getBean("userService")));
	}

	public static void main(String[] args) {
		new ApplicationBuilder().execute();
	}

	public void execute() {
		for (Initialization init : inits) {
			init.init();
		}
	}

	private void addInitialization(Initialization initialization) {
		inits.add(initialization);
	}

	public ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	public void setApplicationContext(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}

}
